#include "FLOAT.h"

FLOAT F_mul_F(FLOAT a, FLOAT b) {
	union {
		struct{
			int int1;
			int int2;
		};
		long long result;
	} longInt;
	FLOAT result;
	longInt.result = (long long)a * b;
	result = (longInt.int1 >> 16) & 0xffff;
	result += longInt.int2 << 16;
	return result;
}

FLOAT F_div_F(FLOAT a, FLOAT b) {
	/* Dividing two 64-bit integers needs the support of another library
	 * `libgcc', other than newlib. It is a dirty work to port `libgcc'
	 * to NEMU. In fact, it is unnecessary to perform a "64/64" division
	 * here. A "64/32" division is enough.
	 *
	 * To perform a "64/32" division, you can use the x86 instruction
	 * `div' or `idiv' by inline assembly. We provide a template for you
	 * to prevent you from uncessary details.
	 *
	 *     asm volatile ("??? %2" : "=a"(???), "=d"(???) : "r"(???), "a"(???), "d"(???));
	 *
	 * If you want to use the template above, you should fill the "???"
	 * correctly. For more information, please read the i386 manual for
	 * division instructions, and search the Internet about "inline assembly".
	 * It is OK not to use the template above, but you should figure
	 * out another way to perform the division.
	 */
	
	int sign, result;

	sign = 1;
	if (a < 0) {
		sign = -sign;
		a = -a;
	} 
	if (b < 0) {
		sign = -sign;
		b = -b;
	}

	asm volatile ("idiv %2, %3;" 
				  : "=a"(result), "=d"(a) 
				  : "r"(b), "a"(a), "d"(0));

	int cnt;
	for (cnt = 0; cnt < 16; ++cnt) {
		a <<= 1;
		result <<= 1;
		if (a >= b) {
			a -= b;
			++result;
		}
	}


	return result * sign;
}

FLOAT f2F(float a) {
	/* You should figure out how to convert `a' into FLOAT without
	 * introducing x87 floating point instructions. Else you can
	 * not run this code in NEMU before implementing x87 floating
	 * point instructions, which is contrary to our expectation.
	 *
	 * Hint: The bit representation of `a' is already on the
	 * stack. How do you retrieve it to another variable without
	 * performing arithmetic operations on it directly?
	 */

	const int  *a_ptr;
	int exp, frac;
	a_ptr = (int *)&a;
	exp = (*a_ptr & 0x7f800000) >> 23;
	frac = *a_ptr & 0x7fffff;
	frac += 0x800000;
	if (exp >= 0x80) {
		if (exp - 0x80 >= 6) {
			frac <<= exp - 0x80 - 6;
		} else {
			frac >>= 0x80 + 6 - exp;
		}
	} else {
		frac >>= 0x80 - exp + 6;
	}
	if (*a_ptr >> 31) {
		frac = -frac;
	}
	return frac;
}

FLOAT Fabs(FLOAT a) {
	if (a & 0x80000000) {
		a = -a;
	}
	return a;
}

/* Functions below are already implemented */

FLOAT sqrt(FLOAT x) {
	FLOAT dt, t = int2F(2);

	do {
		dt = F_div_int((F_div_F(x, t) - t), 2);
		t += dt;
	} while(Fabs(dt) > f2F(1e-4));

	return t;
}

FLOAT pow(FLOAT x, FLOAT y) {
	/* we only compute x^0.333 */
	FLOAT t2, dt, t = int2F(2);

	do {
		t2 = F_mul_F(t, t);
		dt = (F_div_F(x, t2) - t) / 3;
		t += dt;
	} while(Fabs(dt) > f2F(1e-4));

	return t;
}

